import pyNastran
from pyNastran.op2.op2 import read_op2


def parse_op2_items(op2_path):
    # 解析subcaseID和输出物理量名称
    model = read_op2(op2_path)
    stats = model.get_op2_stats()
    valid_items = []
    for line in stats.split("\n"):
        if not line.startswith(" ") and line != "":
            index = line.index("[")
            isubcase = int(line[index + 1])
            valid_items.append(
                {
                    "name": f"{line[:index]}@{isubcase}",
                    "mdoLabel": f"{line[:index]}_{isubcase}",
                    "value": "",
                    "type": "array",
                }
            )
    return valid_items


def extarct_op2_result(op2_path, output):
    item = output["name"].split("@")[0]
    isubcase = int(output["name"].split("@")[1])
    model = read_op2(op2_path)
    return model.get_result(item)[isubcase].data.tolist()

